import React, { useEffect, useState } from 'react';

export default function useEffectHook() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    console.log('执行有副作用操作');
    clearInterval(window.timer);
    window.timer = setInterval(() => {
      setCount(count + 1);
    }, 2000);
    return () => {
      console.log('触发清除机制');
      clearInterval(window.timer);
    };
  });
  return (
    <div>
      <h1>Effect Hook</h1>
      <p>计数：{count}</p>
      <button
        onClick={() => {
          setCount(count + 1);
        }}
      >
        点击+1
      </button>
    </div>
  );
}
